<template>{{$lens := .table.Columns|len}}
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">{{range $index, $column := .table.Columns}}{{if eq $column.IsQuery "1"}}{{if eq $column.HtmlType "input"}}
        <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.ColumnName}}">
            <el-input
                v-model="queryParams.{{$column.ColumnName}}"
                placeholder="请输入{{$column.ColumnComment}}"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
            />
        </el-form-item>{{else if eq $column.HtmlType "select" "radio"}}{{if ne $column.DictType ""}}
        <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.ColumnName}}">
            <el-select v-model="queryParams.{{$column.ColumnName}}" placeholder="请选择{{$column.ColumnComment}}" clearable size="small">
                <el-option
                    v-for="dict in {{$column.ColumnName}}Options"
                    :key="dict.dict_value"
                    :label="dict.dict_label"
                    :value="dict.dict_value"
                />
            </el-select>
        </el-form-item>{{else}}
        <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.ColumnName}}">
            <el-select v-model="queryParams.{{$column.ColumnName}}" placeholder="请选择{{$column.ColumnComment}}" clearable size="small">
                <el-option label="请选择字典生成" value="" />
            </el-select>
        </el-form-item>{{end}}{{else if eq $column.HtmlType "datetime"}}
        <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.ColumnName}}">
            <el-date-picker
                    clearable size="small" style="width: 200px"
                    v-model="queryParams.{{$column.ColumnName}}"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择{{$column.ColumnComment}}">
            </el-date-picker>
        </el-form-item>{{end}}{{end}}{{end}}
      <el-form-item>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['POST:/business/{{.table.BusinessName}}']"
        >新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="{{.table.BusinessName}}List"
      row-key="{{.table.TreeCode}}"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >{{range $index, $column := .table.Columns}}{{if eq $column.IsPk "1"}}
        <el-table-column label="{{$column.ColumnComment}}" align="center" prop="{{$column.ColumnName}}" />{{else if and (eq $column.IsList "1") (eq $column.HtmlType "datetime")}}
        <el-table-column label="{{$column.ColumnComment}}" align="center" prop="{{$column.ColumnName}}" width="180">
            <template slot-scope="scope">
                <span>\{\{ parseTime(scope.row.{{$column.ColumnName}}, {y}-{m}-{d}) \}\}</span>
            </template>
        </el-table-column>{{else if and (eq $column.IsList "1") (ne $column.DictType "")}}
        <el-table-column label="{{$column.ColumnComment}}" align="center" prop="{{$column.ColumnName}}" :formatter="{{$column.ColumnName}}Format" />{{else if and (eq $column.IsList "1") (ne $column.ColumnName "")}}
        <el-table-column label="{{$column.ColumnComment}}" align="center" prop="{{$column.ColumnName}}" />{{end}}{{end}}
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['PUT:/business/{{.table.BusinessName}}']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['DELETE:/business/{{.table.BusinessName}}']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
      {{$treeParentCode := .table.TreeParentCode}}{{$treeCode :=.table.TreeCode}}{{$businessName :=.table.BusinessName}}{{$treeName :=.table.TreeName}}
    <!-- 添加或修改{{.table.FunctionName}}对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">{{range $index, $column := .table.Columns}}{{if and (eq $column.IsInsert "1") (ne $column.IsPk "1")}}{{if and (ne $treeParentCode "") (eq $column.ColumnName $treeParentCode)}}
          <el-form-item label="{{$column.ColumnComment}}" prop="{{$treeParentCode}}">
              <treeselect v-model="form.{{$treeParentCode}}" :options="{{$businessName}}Options" :normalizer="normalizer" placeholder="请选择{{$column.ColumnComment}}" />
          </el-form-item>{{else if eq $column.HtmlType "input"}}
          <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.ColumnName}}">
              <el-input v-model="form.{{$column.ColumnName}}" placeholder="请输入{{$column.ColumnComment}}" />
          </el-form-item>{{else if eq $column.HtmlType "select" }}{{if ne $column.DictType ""}}
          <el-form-item label="{{$column.ColumnComment}}">
              <el-select v-model="form.{{$column.ColumnName}}" placeholder="请选择{{$column.ColumnComment}}">
                  <el-option
                      v-for="dict in {{$column.ColumnName}}Options"
                      :key="dict.dict_value"
                      :label="dict.dict_label"{{if eq $column.GoType "Integer"}}
                      :value="parseInt(dict.dict_value)"{{else}}
                      :value="dict.dict_value"{{end}}
                  ></el-option>
              </el-select>
          </el-form-item>{{else}}
          <el-form-item label="{{$column.ColumnComment}}">
              <el-select v-model="form.{{$column.ColumnName}}" placeholder="请选择{{$column.ColumnComment}}">
                  <el-option label="请选择字典生成" value="" />
              </el-select>
          </el-form-item>{{end}}{{else if eq $column.HtmlType "radio" }}{{if ne $column.DictType ""}}
          <el-form-item label="{{$column.ColumnComment}}">
              <el-radio-group v-model="form.{{$column.ColumnName}}">
                  <el-radio
                      v-for="dict in {{$column.ColumnName}}Options"
                      :key="dict.dict_value"{{if eq $column.GoType "Integer"}}
                      :value="parseInt(dict.dict_value)"{{else}}
                      :value="dict.dict_value"{{end}}
                  >\{\{$dict.dict_label\}\}</el-radio>
              </el-radio-group>
          </el-form-item>{{else}}
          <el-form-item label="{{$column.ColumnComment}}">
              <el-radio-group v-model="form.{{$column.ColumnName}}">
                  <el-radio label="1">请选择字典生成</el-radio>
              </el-radio-group>
          </el-form-item>{{end}}{{else if eq $column.HtmlType "datetime"}}
          <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.ColumnName}}">
              <el-date-picker
                  clearable size="small" style="width: 200px"
                  v-model="form.{{$column.ColumnName}}"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择{{$column.ColumnComment}}">
              </el-date-picker>
          </el-form-item>{{else if eq $column.HtmlType "textarea"}}
          <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.ColumnName}}">
              <el-input v-model="form.{{$column.ColumnName}}" type="textarea" placeholder="请输入内容" />
          </el-form-item>{{end}}{{end}}{{end}}
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list{{.table.BusinessName}}, get{{.table.BusinessName}}, del{{.table.BusinessName}}, add{{.table.BusinessName}}, update{{.table.BusinessName}}, export{{.table.BusinessName}} } from "@/api/business/{{.table.BusinessName}}";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "{{.table.BusinessName}}",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // {{.table.FunctionName}}表格数据
      {{.table.BusinessName}}List: [],
      // {{.table.FunctionName}}树选项
      {{.table.BusinessName}}Options: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,{{range $index, $column := .table.Columns}}{{if ne $column.DictType ""}}
      // $comment字典
      {{$column.ColumnName}}Options: [],{{end}}{{end}}
      // 查询参数
      queryParams: { {{range $index, $column := .table.Columns}}{{if eq $column.IsQuery "1"}}
        {{$column.ColumnName}}: undefined{{if ne $lens $index}},{{end}}{{end}}{{end}}
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: { {{range $index, $column := .table.Columns}}{{if eq $column.IsRequired "1"}}
        {{$column.ColumnName}} : [
          { required: true, message: "{{$column.ColumnComment}}不能为空", trigger: "blur" }
        ]{{if ne $lens $index}},{{end}}{{end}}{{end}}
      }
    };
  },
  created() {
    this.getList(); {{range $index, $column := .table.Columns}}{{if ne $column.DictType ""}}
    this.getDicts("{{$column.DictType}}").then(response => {
      this.{{$column.ColumnName}}Options = response.data;
    });{{end}}{{end}}
  },
  methods: {
    /** 查询{{.table.FunctionName}}列表 */
    getList() {
      this.loading = true;
      list{{.table.BusinessName}}(this.queryParams).then(response => {
        this.{{.table.BusinessName}}List = this.handleTree(response.data, "{{$treeCode}}", "{{$treeParentCode}}");
        this.loading = false;
      });
    },
    /** 转换{{.table.FunctionName}}数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.{{$treeCode}},
        label: node.{{$treeName}},
        children: node.children
      };
    },
    /** 查询{{.table.FunctionName}}下拉树结构 */
    getTreeselect() {
      list{{.table.BusinessName}}().then(response => {
        this.{{.table.BusinessName}}Options = [];
        const data = { {{$treeCode}}: 0, {{$treeName}}: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "{{$treeCode}}", "{{$treeParentCode}}");
        this.{{.table.BusinessName}}Options.push(data);
      });
    },{{range $index, $column := .table.Columns}}{{if ne $column.DictType ""}}
      // $comment字典翻译
    {{$column.ColumnName}}Format(row, column) {
        return this.selectDictLabel(this.{{$column.ColumnName}}Options, row.{{$column.ColumnName}});
    },{{end}}{{end}}
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = { {{range $index, $column := .table.Columns}}{{if eq $column.HtmlType "radio"}}
        {{$column.ColumnName}}: "0"{{if ne $lens $index}},{{end}}{{else}}
        {{$column.ColumnName}}: undefined{{if ne $lens $index}},{{end}}{{end}}{{end}}
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
	  this.getTreeselect();
      this.open = true;
      this.title = "添加{{.table.FunctionName}}";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
	  this.getTreeselect();
      if (row != undefined) {
        this.form.{{$treeParentCode}} = row.{{$treeCode}};
      }
      get{{.table.BusinessName}}(row.{{.table.PkColumn.ColumnName}}).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改{{.table.FunctionName}}";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.{{.table.PkColumn.ColumnName}} != undefined) {
            update{{.table.BusinessName}}(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            add{{.table.BusinessName}}(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除{{.table.FunctionName}}编号为"' + row.{{.table.PkColumn.ColumnName}} + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return del{{.table.BusinessName}}(row.{{.table.PkColumn.ColumnName}});
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    }
  }
};
</script>